package com.shure.surdes.modules.survey.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shure.surdes.common.query.condition.Condition;
import com.shure.surdes.common.result.R;
import com.shure.surdes.common.result.Result;
import com.shure.surdes.modules.survey.domain.AnswerJson;
import com.shure.surdes.modules.survey.domain.Survey;
import com.shure.surdes.modules.survey.query.SurveyQuery;
import com.shure.surdes.modules.survey.service.IAnswerJsonService;
import com.shure.surdes.modules.survey.service.ISurveyService;
import com.shure.surdes.modules.survey.vo.SurveyVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;


/**
 * 问卷Controller
 *
 * @author Shure
 * @date 2021-10-18
 */
@RestController
@RequestMapping("/survey/survey")
public class SurveyController {

    @Autowired
    private ISurveyService surveyService;
    @Autowired
    private IAnswerJsonService answerJsonService;


    /**
     * 查询问卷列表
     */
    @GetMapping("/list")
    public R<Page<Survey>> page(@Validated SurveyQuery query) {
        LambdaQueryWrapper<Survey> wrapper = Condition.getLambdaQueryWrapper1(query);
        wrapper.orderByDesc(Survey::getCreateTime);
        Page<Survey> page = surveyService.page(query.getPage(), wrapper);
        return Result.ok(page);
    }

    /**
     * 获取问卷详细信息
     */
    @GetMapping(value = "/{surveyId}")
    public R<Survey> detail(@PathVariable("surveyId") Long surveyId) {
        return Result.ok(surveyService.getById(surveyId));
    }

    /**
     * 新增/更新问卷
     */
    @PostMapping
    public R saveOrUpdate(@RequestBody Survey survey) {
        return Result.ok(surveyService.saveOrUpdate(survey));
    }

    /**
     * 从模板复制问卷
     */
    @PostMapping("/save/template")
    public R saveByTemplate(@RequestBody Survey survey) {
        return Result.ok(surveyService.saveByTemplate(survey));
    }

    /**
     * 删除问卷
     */
    @PutMapping("/remove/{surveyIds}")
    public R remove(@PathVariable Long[] surveyIds) {
        return Result.ok(surveyService.removeSurveyBySurveyIds(surveyIds));
    }

    /**
     * 还原问卷
     */
    @PutMapping("/restore/{surveyIds}")
    public R restore(@PathVariable Long[] surveyIds) {
        return Result.ok(surveyService.restoreSurveyBySurveyIds(surveyIds));
    }

    /**
     * 永久删除问卷
     */
    @DeleteMapping("/{surveyIds}")
    public R delete(@PathVariable Long[] surveyIds) {
        return Result.ok(surveyService.deleteSurveyBySurveyIds(surveyIds));
    }

    /**
     * 发布问卷
     */
    @PutMapping("/publish/{surveyIds}")
    public R publish(@PathVariable Long[] surveyIds) {
        return Result.ok(surveyService.publishSurveyBySurveyIds(surveyIds));
    }

    /**
     * 撤销发布问卷
     */
    @PutMapping("/revoke/{surveyId}")
    public R revoke(@PathVariable Long surveyId) {
        List<AnswerJson> answerJsons = answerJsonService.answerJsonBySurvey(surveyId);
        if (!answerJsons.isEmpty()) {
            return Result.error("数据已采集，不能撤销");
        }
        return Result.ok(surveyService.revokeSurveyBySurveyId(surveyId));
    }

    /**
     * 复制问卷
     */
    @PostMapping("/copy/{surveyId}")
    public R copy(@PathVariable Long surveyId) {
        return Result.ok(surveyService.copySurveyBySurveyId(surveyId));
    }

    /** ========== 数据分析相关的接口 */

    @GetMapping("/analysis/list")
    public R analysisList(SurveyQuery query) {
        Page<SurveyVo> page = surveyService.analysisList(query.getPage(), query);
        return Result.ok(page);
    }
    

}
